// pages/report/report.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    page: 1,
    hide_text: true,
    rank: 0,
    show_rank_input: false
  },

  onTap: function (e) {
    wx.reLaunch({
      url: '/pages/demo/demo?id=' + e.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    wx.showLoading({
      title: '读取中',
    })
    app.request({
      url: app.root + 'report',
      success: function (res) {
        that.setData({
          items: res.data.items,
          show_rank_btn: res.data.rank,
          hide_text: res.data.items ? true : false,
          rank: res.data.rank - 1,
          rank_update: res.data.rank_update,
          rank_list: res.data.rank_list
        })
        wx.hideLoading()
      }
    })
  },

  onRankBtn: function (e) {
    if(this.data.rank_update){
      this.setData({
        show_rank_input: true
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '一周之内修改一次，还没到时间哦',
      })
    }
  },

  onRankSelect: function (e) {
    this.setData({
      rank: e.detail.value
    })
  },

  onRankConfirm: function(e){
    if(this.rlock){
      return
    }
    this.rlock = true

    wx.showLoading({
      title: '修改中',
    })

    const that = this
    app.request({
      url: app.root + 'round/rank',
      method: 'post',
      data:{
        rank: parseInt(this.data.rank) + 1
      },
      success: function(res){
        wx.showModal({
          title: '提示',
          content: res.data.msg,
        })
        that.setData({
          show_rank_input: false,
          rank_update: false
        })

        wx.hideLoading()
        that.rlock = false
      }

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this
    app.request({
      url: app.root + 'report',
      success: function (res) {
        that.setData({
          items: res.data.items,
          page: 1
        })
        wx.stopPullDownRefresh()
      }
    })

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.no_more){
      wx.showLoading({
        title: '没有更多了..',
      })
      return
    }
    const that = this
    if (that.plock == true) {
      return
    }
    that.plock = true
    wx.showLoading({
      title: '读取中',
    })

    app.request({
      url: app.root + 'report',
      data: {
        page: that.data.page += 1
      },
      success: function (res) {
        that.setData({
          no_more: res.data.err,
          items: that.data.items.concat(res.data.items)
        })
        that.plock = false
        wx.hideLoading()
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})